{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1.11 \u547d\u540d\u5207\u7247\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u5305\u542b\u4e86\u5927\u91cf\u65e0\u6cd5\u76f4\u89c6\u7684\u786c\u7f16\u7801\u5207\u7247\uff0c\u5e76\u4e14\u4f60\u60f3\u6e05\u7406\u4e00\u4e0b\u4ee3\u7801\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5047\u5b9a\u4f60\u8981\u4ece\u4e00\u4e2a\u8bb0\u5f55\uff08\u6bd4\u5982\u6587\u4ef6\u6216\u5176\u4ed6\u7c7b\u4f3c\u683c\u5f0f\uff09\u4e2d\u7684\u67d0\u4e9b\u56fa\u5b9a\u4f4d\u7f6e\u63d0\u53d6\u5b57\u6bb5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "######    0123456789012345678901234567890123456789012345678901234567890'\nrecord = '....................100 .......513.25 ..........'\ncost = int(record[20:23]) * float(record[31:37])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0e\u5176\u90a3\u6837\u5199\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u60f3\u8fd9\u6837\u547d\u540d\u5207\u7247\u5462\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "SHARES = slice(20, 23)\nPRICE = slice(31, 37)\ncost = int(record[SHARES]) * float(record[PRICE])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u4f60\u907f\u514d\u4e86\u4f7f\u7528\u5927\u91cf\u96be\u4ee5\u7406\u89e3\u7684\u786c\u7f16\u7801\u4e0b\u6807\u3002\u8fd9\u4f7f\u5f97\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u53ef\u8bfb\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u822c\u6765\u8bb2\uff0c\u4ee3\u7801\u4e2d\u5982\u679c\u51fa\u73b0\u5927\u91cf\u7684\u786c\u7f16\u7801\u4e0b\u6807\u4f1a\u4f7f\u5f97\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u5927\u5927\u964d\u4f4e\u3002\n\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u56de\u8fc7\u6765\u770b\u770b\u4e00\u5e74\u524d\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u4f60\u4f1a\u6478\u7740\u8111\u888b\u60f3\u90a3\u65f6\u5019\u81ea\u5df1\u5230\u5e95\u60f3\u5e72\u561b\u554a\u3002\n\u8fd9\u662f\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5b83\u8ba9\u4f60\u66f4\u52a0\u6e05\u6670\u7684\u8868\u8fbe\u4ee3\u7801\u7684\u76ee\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5185\u7f6e\u7684 slice() \u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u5207\u7247\u5bf9\u8c61\u3002\u6240\u6709\u4f7f\u7528\u5207\u7247\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u5bf9\u8c61\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "items = [0, 1, 2, 3, 4, 5, 6]\na = slice(2, 4)\nitems[2:4]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "items[a]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "items[a] = [10,11]\nitems"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "del items[a]\nitems"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5207\u7247\u5bf9\u8c61a\uff0c\u4f60\u53ef\u4ee5\u5206\u522b\u8c03\u7528\u5b83\u7684 a.start , a.stop , a.step \u5c5e\u6027\u6765\u83b7\u53d6\u66f4\u591a\u7684\u4fe1\u606f\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = slice(5, 50, 2)\na.start"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a.stop"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a.step"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u5207\u7247\u7684 indices(size) \u65b9\u6cd5\u5c06\u5b83\u6620\u5c04\u5230\u4e00\u4e2a\u5df2\u77e5\u5927\u5c0f\u7684\u5e8f\u5217\u4e0a\u3002\n\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u4e09\u5143\u7ec4 (start, stop, step) \uff0c\u6240\u6709\u7684\u503c\u90fd\u4f1a\u88ab\u7f29\u5c0f\uff0c\u76f4\u5230\u9002\u5408\u8fd9\u4e2a\u5df2\u77e5\u5e8f\u5217\u7684\u8fb9\u754c\u4e3a\u6b62\u3002\n\u8fd9\u6837\uff0c\u4f7f\u7528\u7684\u65f6\u5c31\u4e0d\u4f1a\u51fa\u73b0 IndexError \u5f02\u5e38\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = 'HelloWorld'\na.indices(len(s))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for i in range(*a.indices(len(s))):\n    print(s[i])"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}